﻿/***************************************************
 * File name: ShoppingCartController.cs 
 * Author: Team A
 * Change Log: Please see SVN repository: http://test-project-drew1.googlecode.com/svn/trunk/
 * Date: December 2013
 * Description: See <summary> tab below
 * *************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WSCeCommerceWeb.Models;
using WSCeCommerce.Helpers;

namespace WSCeCommerceWeb.Controllers
{
    public class ShoppingCartController : Controller
    {

        private const string SESSION_SALE_KEY = "Session.Sale";

        // Customization: Renamed to be consistant with naming standards
        private WSCeCommerceDBContext _db = new WSCeCommerceDBContext();

        //
        // GET: /ShoppingCart/

        public ActionResult Index()
        {
            return View(ShoppingCart.GetCart(this));
        }



        [HttpPost]
        public ActionResult UpdateQuantity(int id, int Quantity)
        {
           // Retreive item being updated from the database
            var updatedItem = _db.Items
              .Single(Item => Item.ItemId == id);
            
            //Get the shopping cart
            var cart = ShoppingCart.GetCart(this.HttpContext);
           
            //Update quantity of selected item
            int itemCount = cart.UpdateCart(id, Quantity);

            // Return a successful result
            return Json(new { ok = true, message = String.Format(updatedItem.Name + " successfully updated to: " + Quantity), Quantity = itemCount });
        }
        
        [HttpPost]
        public ActionResult AddToCart(int id)
        {
            // Retrieve the item from the database
            var addedItem = _db.Items
                .Single(Item => Item.ItemId == id);

            // Add it to the shopping cart
            var cart = ShoppingCart.GetCart(this.HttpContext);
            int itemCount = cart.AddToCart(addedItem);

            // Return a successful result
            return Json(new { ok = true, message = String.Format("'{0}' added to cart.", addedItem.Name), count = itemCount,itemId = addedItem.ItemId });
        }


        [HttpPost]
        public ActionResult RemoveFromCart(int id)
        {
            // Remove the item from the cart
            var cart = ShoppingCart.GetCart(this.HttpContext);

            // Get the name of the item to display confirmation
            // Retrieve the item from the database
            var addedItem = _db.Items
                .Single(Item => Item.ItemId == id);

            // Remove from cart
            int itemCount = cart.RemoveFromCart(id);

            return Json(new { ok = true, count = itemCount, itemId = addedItem.ItemId });
        }
        
        [ChildActionOnly]
        public ActionResult CartSummary()
        {
            var cart = ShoppingCart.GetCart(this.HttpContext);

            ViewData["CartCount"] = cart.GetCount();

            return PartialView("CartSummary");
        }
    }
}